The purpose of this exercise is to check the maximum sea surface temparature near
Where can we look for the data? http://www.bio-oracle.org/
First, we load some R packages that we will use in this exercise ```
library(sf) # spatial package
library(ggplot2) # plotting
library(sdmpredictors) # species distribution modeling layers => data layers
Now, we want to use the data from the sdmpredictors package
sdmpredictors contains several datasets, each containing several layers:
# list datasets of sdmpredictors package
list_datasets()
# list layers of Bio-ORACLE dataset
list_layers(datasets="Bio-ORACLE")
Now we store the data from the layer to R:
sstmax <- load_layers("BO_sstmax")
Let’s try R’s base plot:
# base plot
plot(sstmax)
This is not so user friendly, let’s try to make an interactive map.
With the ‘mapview’ package, you can easily create interactive maps. For more info, see https://r-spatial.github.io/mapview/
# Make an interactive plot with the 'mapview' package:
# more info: https://r-spatial.github.io/mapview/
library('mapview')
if we just try mapview(sstmax), there is an error. What does it say?
mapview(sstmax, layer.name = "max sst") # warning: what does it say?
mapview(sstmax, layer.name = "max sst", maxpixels = 9331200) # takes a while to load!
Let’s create a dataframe with our points of interest:
my.sites <- data.frame(
Name = c("Key West", "Barbados", "Porto"),
Lon = c(-81.775,-59.440,-8.882),
Lat = c(24.545,13.185,41.130))
# make spatial (simple features) dataset
my.sites.sf <- st_as_sf(my.sites,
coords=c("Lon","Lat"),
crs = 4326) # WGS84
Again, R’s base plotting capabilities are not ideal…
# base plot
plot(my.sites.sf) # not ideal
##### 1.3.3 interactive mapview map An interactive map is a lot better, right?
# interactive mapview map
mapview(my.sites.sf) # much better
How do we visualize both of the layers? Just combine them with a plus sign: ‘+’:
# how to visualize both? combine them with '+'
mapview(sstmax, layer.name = "max sst") +
mapview(my.sites.sf)
Now, we want to get the sea surface temperature data at the exact location of our points. This is easily done with the raster package function extract:
# extract data from sstmax raster at the locations of my.sites.sf
my.sites$BO_sstmax <- extract(sstmax,my.sites.sf)
We will plot the data that we have with the ggplot2 package: https://ggplot2.tidyverse.org/
The grammar of graphics say that can build any graph with the same components:
# plotting data with ggplot2
# https://ggplot2.tidyverse.org/
library(ggplot2)
Let’s create an easy example with
my.sitesName column, y-axis BO_sstmax (the temperature)# points
ggplot(data = my.sites) +
geom_point(aes(x = Name, y = BO_sstmax))
If we improve the graph by:
# with different colors, a bit larger size
ggplot(data = my.sites) +
geom_point(aes(x = Name,
y = BO_sstmax,
color = BO_sstmax,
size = 2)) +
scale_color_viridis_c(option = 'inferno') # same colorscale as map
We can change the geometry to a barplot:
# bar plot
ggplot(data = my.sites) +
geom_col(aes(x = Name, y = BO_sstmax))
different colors, larger size (outline width):
# again different colors, larger size:
ggplot(data = my.sites) +
geom_col(aes(x = Name, # everything inside aes(), will be 'interpreted'
y = BO_sstmax,
fill = Name),
size = 2,
color = "black") # color : outline with graphs
Some more adjustments
# color by sst max
ggplot(data = my.sites) +
geom_col(aes(x = Name, # everything inside aes(), will be 'interpreted'
y = BO_sstmax,
fill = BO_sstmax),
color = "black") +
scale_fill_viridis_c(option = 'inferno') # color : outline with graphs
Adjust the code above to include 3 areas of your interest
e.g. the location closest you your lab?
What is the temperature at different latitudes in the North Atlantic?
e.g. at longitude -30, latitude from 0 to 60?
What is the minimum temperature? Can you plot: